package com.dkd.service.impl;

import cn.elegent.pay.CallBackService;
import cn.elegent.pay.dto.WatchDTO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dkd.config.constant.OrderStatus;
import com.dkd.config.constant.PayStatus;
import com.dkd.entity.OrderEntity;
import com.dkd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DiXiaoNie
 * @since 2023/2/4 16:03
 */
@Service
@Slf4j
public class CallBackServiceImpl implements CallBackService {

    @Autowired
    private OrderService orderService;

    /**
     * 订单成功支付的逻辑
     *
     * @param orderSn
     */
    @Override
    public void successPay(String orderSn) {
        log.info("支付成功回调{}", orderSn);
        //查询订单
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderSn);
        if (orderEntity != null) {
            orderEntity.setStatus(OrderStatus.ORDER_STATUS_PAYED);
            orderEntity.setPayStatus(PayStatus.PAY_STATUS_PAYED);
            this.orderService.updateById(orderEntity);
            //todo: 发货
            this.orderService.vendout(orderSn, orderEntity.getSkuId(), orderEntity.getInnerCode(), orderEntity.getAmount());
        }
    }

    /**
     * 支付失败日志记录
     *
     * @param orderSn
     */
    @Override
    public void failPay(String orderSn) {
        log.info("支付失败回调！" + orderSn);
    }

    /**
     * 需要查询订单状态为已支付返回true
     *
     * @param orderSn
     * @return
     */
    @Override
    public boolean isSuccessPay(String orderSn) {
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderSn);
        if (orderEntity != null && orderEntity.getPayStatus().equals(OrderStatus.ORDER_STATUS_PAYED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改订单状态为退款完成
     *
     * @param orderSn
     */
    @Override
    public void successRefund(String orderSn) {
        log.info("退款成功回调{}", orderSn);
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderSn);
        if (orderEntity != null) {
            //将状态设置为退款
            orderEntity.setPayStatus(PayStatus.PAY_STATUS_REFUNDIED);
            this.orderService.updateById(orderEntity);
        }
    }

    /**
     * 退款失败打日志记录--修改状态为已支付
     *
     * @param orderSn
     */
    @Override
    public void failRefund(String orderSn) {
        log.info("退款失败回调{}", orderSn);
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderSn);
        if (orderEntity != null) {
            orderEntity.setPayStatus(OrderStatus.ORDER_STATUS_PAYED);
            this.orderService.updateById(orderEntity);
        }
    }

    /**
     * 查询订单状态是否为退款完成
     *
     * @param orderSn
     * @return
     */
    @Override
    public boolean isSuccessRefund(String orderSn) {
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderSn);
        if (orderEntity != null && orderEntity.getPayStatus().equals(PayStatus.PAY_STATUS_REFUNDIED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询所有未支付订单
     *
     * @return
     */
    @Override
    public List<WatchDTO> watchPayOrderSnList() {
        return this.orderService.list(Wrappers.<OrderEntity>lambdaQuery()
                //订单为未支付状态
                .eq(OrderEntity::getStatus, OrderStatus.ORDER_STATUS_CREATE))
                .stream()
                .map(order -> {
                    WatchDTO watchDTO = new WatchDTO();
                    //订单编号
                    watchDTO.setOrderSn(order.getOrderNo());
                    //平台
                    watchDTO.setPlatform(order.getPayType());
                    return watchDTO;
                }).collect(Collectors.toList());
    }
}
